<template>
  <div>
    <address-header>
      <span v-if="this.key == true">添加地址</span>
      <span v-else>编辑地址</span>
    </address-header>
    <section>
      <!-- 添加地址 -->
      <div v-if="this.key == true">
        <van-address-edit
          :area-list="areaList"
          show-set-default
          show-search-result
          :search-result="searchResult"
          @save="onAdd"
        />
      </div>
      <!-- 编辑地址 -->
      <div v-else>
        <van-address-edit
          :area-list="areaList"
          show-delete
          :address-info="AddressEditInfo"
          show-set-default
          show-search-result
          @save="onEdit"
          @delete="onDelete"
        />
      </div>
    </section>
  </div>
</template>

<script>
import addressHeader from "./Header.vue";
import qs from "qs";
import { Dialog } from "vant";
import { Toast } from "vant";
export default {
  name: "AddressList",
  components: {
    addressHeader,
  },
  data() {
    return {
      areaList: {
        province_list: {
          110000: "北京市",
          120000: "天津市",
        },
        city_list: {
          110100: "北京市",
          120100: "天津市",
        },
        county_list: {
          110101: "东城区",
          110102: "西城区",
          120101: "关安渠",
          // ....
        },
      },
      searchResult: [],
      key: false,
      AddressEditInfo: {},
      delId: {
        id: 0, //删除订单的id
      },
    };
  },
  created() {
    let params = this.$route.params;
    if (JSON.parse(params.key) == "add") {
      this.key = true;
    } else {
      //编辑地址
      this.AddressEditInfo = JSON.parse(params.key);
      // console.log(this.AddressEditInfo.isDefault);
      this.AddressEditInfo.isDefault =
        this.AddressEditInfo.isDefault == "1" ? true : false;
    }
  },
  methods: {
    //点击保存 添加地址
    onAdd(onSave) {
      onSave.isDefault = onSave.isDefault == true ? "1" : "0";
      // console.log(onSave.isDefault);
      //获取当前登录用户的token
      const token = window.sessionStorage.getItem("token");
      onSave = Object.assign({}, onSave, { token: token });
      // console.log(onSave);
      this.$http.post("/addAddress", qs.stringify(onSave)).then((res) => {
        console.log(res);
      });
      this.$router.push("/address/addressIndex");
    },

    //点击保存 编辑地址
    onEdit(onSave) {
      console.log(onSave);
      onSave.isDefault = onSave.isDefault == true ? "1" : "0";
      const token = window.sessionStorage.getItem("token");
      onSave = Object.assign({}, onSave, { token: token });
      this.$http.post("/updateAddress", qs.stringify(onSave)).then((res) => {
        console.log(res);
      });
      this.$router.push("/address/addressIndex");
    },

    //删除地址
    onDelete(content) {
      // console.log(content);
      this.delId.id = content.id;
      Dialog.confirm({
        title: "确定删除地址吗？",
        message: "此操作将删除用户地址，是否继续？",
      })
        .then(() => {
          // on confirm
          this.$http
            .post("/deleteAddress", qs.stringify(this.delId))
            .then((res) => {
              console.log(res);
            });
          Toast("delete");
          this.$router.back();
        })
        .catch(() => {
          // on cancel
        });
    },
  },
};
</script>